package com.nice.demo.test;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.nice.demo.R;

/**
 * author : jc
 * date   : 2020/3/16
 * desc   :
 * version: 1.0
 */
public class DemoActivity extends AppCompatActivity {
    private WebView mWebView;
    private FrameLayout frame;
    private MyHandWritingView mHandWritingView;
    private String dataStr = "<html><body>" +
            "<style>img{ width:100% !important;}</style>"
            + "<img src=\"https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/tam-ogel/dd9d1d686cdc814db9653b254e00402e_259_194.jpg\" alt=\"\" /> \r" +
            "<img src=\"https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/tam-ogel/dd9d1d686cdc814db9653b254e00402e_259_194.jpg\" alt=\"\" /> \r" +
            "<p style=\"text-align:right;\">\r\t品类定位的思考\r</p>\r" +
            "<h3>\r\t<strong><span style=\"color:#00D5FF;\">品类定</span></strong>\n" +
            "<h3>\r\t" +
            "<a href='javascript:mobile.testToast(\"点击\")'>点击我跳回APP</a>"
            + "</body></html>";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        mWebView = findViewById(R.id.web);
        frame = findViewById(R.id.frame);
        initWebView();
    }

    /**
     * 设置WebView自适应屏幕
     */
    public void initWebView() {
        WebSettings settings = mWebView.getSettings();

        //settings.setUseWideViewPort(true);//调整到适合webview的大小，不过尽量不要用，有些手机有问题
        settings.setLoadWithOverviewMode(true);//设置WebView是否使用预览模式加载界面。
        mWebView.setVerticalScrollBarEnabled(false);//不能垂直滑动
        mWebView.setHorizontalScrollBarEnabled(false);//不能水平滑动
        settings.setTextSize(WebSettings.TextSize.LARGEST);//通过设置WebSettings，改变HTML中文字的大小
        settings.setJavaScriptCanOpenWindowsAutomatically(true);//支持通过JS打开新窗口
        //设置WebView属性，能够执行Javascript脚本
        mWebView.getSettings().setJavaScriptEnabled(true);//设置js可用
        mWebView.addJavascriptInterface(new Mobile(), "mobile");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                String js = "javascript:mobile.onGetWebContentHeight()";
                view.loadUrl(js);
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onConsoleMessage(ConsoleMessage cm) {
                Log.e("jc", cm.
                        message()
                        + " -- From line "
                        + cm.
                        lineNumber()
                        + " of "
                        + cm.
                        sourceId());
                return true;
            }
        });
//        mWebView.addJavascriptInterface(new AndroidJavaScript(getApplication()), "android");//设置js接口
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//支持内容重新布局


/******  22222222  ***********************************************************************/
//        data = "</Div><head><style>img{ width:100% !important;}</style></head>"+data;//给图片设置一个样式，宽满屏
/******  2222222222  ***********************************************************************/

        mWebView.loadDataWithBaseURL(null, dataStr, "text/html", "utf-8", null);
    }


    private class Mobile {
        @JavascriptInterface
        public void onGetWebContentHeight() {
            //重新调整webview高度
            mWebView.post(new Runnable() {
                @Override
                public void run() {
                    mWebView.measure(0, 0);
                    int measuredHeight = mWebView.getMeasuredHeight();
                    Log.e("jc", "measuredHeight=" + measuredHeight);
                    addHandleView();
                }
            });

        }

        @JavascriptInterface
        public void returnAndroid(String name) {//从网页跳回到APP，这个方法已经在上面的HTML中写上了
            if (name.isEmpty() || name.equals("")) {
                return;
            }
            Toast.makeText(getApplication(), name, Toast.LENGTH_SHORT).show();
            //这里写你的操作///////////////////////
            //MainActivity就是一个空页面，不影响
//            Intent intent = new Intent(WebDataActivity.this, MainActivity.class);
//            intent.putExtra("name",name);
//            startActivity(intent);
        }

        @JavascriptInterface
        public void testToast(String name) {
            if (TextUtils.isEmpty(name)) {
                return;
            }
        }

    }

    private void addHandleView() {
//        ViewGroup.LayoutParams layoutParams1 = frame.getLayoutParams();
//        layoutParams1.width=mWebView.getMeasuredWidth();
//        layoutParams1.height=mWebView.getMeasuredHeight();
//        frame.setLayoutParams(layoutParams1);

        LinearLayout linearLayout = new LinearLayout(DemoActivity.this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        frame.addView(linearLayout);
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) linearLayout.getLayoutParams();
        layoutParams.width = mWebView.getMeasuredWidth();
        layoutParams.height = mWebView.getMeasuredHeight();
        linearLayout.setLayoutParams(layoutParams);


//        View space1 = new View(this);
//        linearLayout.addView(space1);
//        LinearLayout.LayoutParams spaceParams1 = (LinearLayout.LayoutParams) space1.getLayoutParams();
//        spaceParams1.weight = 1;
////        spaceParams1.height = 0;
//        spaceParams1.width = mWebView.getMeasuredWidth();
//        space1.setLayoutParams(spaceParams1);

        mHandWritingView = new MyHandWritingView(DemoActivity.this);
        mHandWritingView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int toolType = event.getToolType(0);
                if (toolType == MotionEvent.TOOL_TYPE_FINGER) {
                    //手指触摸
                    return true;
                } else if (toolType == MotionEvent.TOOL_TYPE_STYLUS) {
                    //手写笔触摸
                    return false;
                } else {
                    //其他
                    return false;
                }
            }
        });
        mHandWritingView.setToWriting();
        mHandWritingView.setPenColor(Color.BLUE);
        mHandWritingView.setDrawType(DrawType.CURVE);
        mHandWritingView.setBackgroundColor(Color.RED);
        linearLayout.addView(mHandWritingView);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mWebView.getMeasuredWidth(),0);
//        params.width = mWebView.getMeasuredWidth();
//        params.height = mWebView.getMeasuredHeight()/3;
        params.weight=1;
        params.topMargin=20;
        mHandWritingView.setLayoutParams(params);

//        View space2 = new View(this);
//        linearLayout.addView(space2);
//        LinearLayout.LayoutParams spaceParams2 = (LinearLayout.LayoutParams) space2.getLayoutParams();
//        spaceParams2.weight = 2;
////        spaceParams2.height = 0;
//        spaceParams2.width = mWebView.getMeasuredWidth();
//        space2.setLayoutParams(spaceParams2);


        MyHandWritingView mHandWritingView2 = new MyHandWritingView(DemoActivity.this);
        mHandWritingView2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int toolType = event.getToolType(0);
                if (toolType == MotionEvent.TOOL_TYPE_FINGER) {
                    //手指触摸
                    return true;
                } else if (toolType == MotionEvent.TOOL_TYPE_STYLUS) {
                    //手写笔触摸
                    return false;
                } else {
                    //其他
                    return false;
                }
            }
        });
        mHandWritingView2.setToWriting();
        mHandWritingView2.setPenColor(Color.BLUE);
        mHandWritingView2.setDrawType(DrawType.CURVE);
        mHandWritingView2.setBackgroundColor(Color.GREEN);
        linearLayout.addView(mHandWritingView2);
        LinearLayout.LayoutParams params2 = (LinearLayout.LayoutParams) mHandWritingView2.getLayoutParams();
        params2.width = mWebView.getMeasuredWidth();
        params2.height = mWebView.getMeasuredHeight()/3;
        params2.topMargin=20;
//        params2.height = 0;
        mHandWritingView2.setLayoutParams(params2);

//        View space3 = new View(this);
//        linearLayout.addView(space3);
//        LinearLayout.LayoutParams spaceParams3 = (LinearLayout.LayoutParams) space3.getLayoutParams();
//        spaceParams3.weight = 3;
////        spaceParams3.height = 0;
//        spaceParams3.width = mWebView.getMeasuredWidth();
//        space3.setLayoutParams(spaceParams3);


    }
}
